package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.DaoMoneyPoolDao;
import com.sc.nft.dao.FirstOrderSettleDao;
import com.sc.nft.dao.SecondOrderSettleDao;
import com.sc.nft.dao.TradingPoolOrderSettleDao;
import com.sc.nft.entity.DaoMoneyPool;
import com.sc.nft.enums.AllocationTypeEnum;
import com.sc.nft.enums.MarketLevelEnum;
import com.sc.nft.service.DaoMoneyPoolService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 服务接口实现
 *
 * @author zxy
 * @since 2023-12-12 14:20:46
 * @description 商户banner
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DaoMoneyPoolServiceImpl implements DaoMoneyPoolService {
    private final DaoMoneyPoolDao daoMoneyPoolDao;
    private final FirstOrderSettleDao firstOrderSettleDao;
    private final SecondOrderSettleDao secondOrderSettleDao;
    private final TradingPoolOrderSettleDao tradingPoolOrderSettleDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void shareMoney(Integer yearWeek, MarketLevelEnum marketLevel, Long id, Long daoId, AllocationTypeEnum allocationType, BigDecimal amount) {
        DaoMoneyPool daoMoneyPool = daoMoneyPoolDao.getByWeekAndType(daoId, marketLevel, yearWeek, allocationType, false);
        if (ObjectUtil.isNull(daoMoneyPool)) {
            daoMoneyPool = new DaoMoneyPool();
            daoMoneyPool.setDaoId(daoId);
            daoMoneyPool.setMarketLevel(marketLevel);
            daoMoneyPool.setAllocationType(allocationType);
            daoMoneyPool.setAmount(BigDecimal.ZERO);
            daoMoneyPool.setYearWeek(yearWeek);
            daoMoneyPool.setIsSettle(false);
        }
        daoMoneyPool.setAmount(daoMoneyPool.getAmount().add(amount));
        daoMoneyPool.insertOrUpdate();
        switch (marketLevel) {
            case FIRST:
                firstOrderSettleDao.updateSettle(id);
                break;
            case SECOND:
                secondOrderSettleDao.updateSettle(id);
                break;
            case TRANS_POOL:
                tradingPoolOrderSettleDao.updateSettle(id);
                break;
        }

    }


}